<template>
  <section class="todoapp">
    <TodoHeader
      @addList="addList"
      :list="list"
      @selectAll="selectAll"
    ></TodoHeader>
    <TodoMain :list="showList" @removeList="removeList"></TodoMain>
    <TodoFooter
      :list="showList"
      @filterList="filterList"
      @clearAll="clearAll"
    ></TodoFooter>
  </section>
</template>

<script>
import './styles/base.css'
import './styles/index.css'
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  name: 'Day04App',

  data() {
    return {
      list: JSON.parse(localStorage.getItem('list') || '[]'),
      isSel: 'all'
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },

  methods: {
    addList(list) {
      this.list.push(list)
    },
    removeList(id) {
      this.list = this.list.filter((item) => {
        return item.id !== id
      })
    },
    filterList(isSel) {
      // console.log(isSel)
      this.isSel = isSel
    },
    clearAll() {
      this.list = this.list.filter((item) => {
        return item.isDone == false
      })
    },
    selectAll(isAll) {
      this.list.forEach((item) => {
        item.isDone = isAll
      })
    }
  },
  computed: {
    showList() {
      if (this.isSel == 'all') {
        return this.list
      } else if (this.isSel == 'yes') {
        return this.list.filter((item) => {
          return item.isDone == true
        })
      } else {
        return this.list.filter((item) => {
          return item.isDone == false
        })
      }
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem('list', JSON.stringify(newVal))
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
